/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of Game packet.
 *
 * Game packet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Game packet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Game packet.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.game.logging;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;

import fi.honkalampisaatio.game.xml.UserSetting;


public class GameLogger {

	public static GameLogger INSTANCE;
	
	public static GameLogger getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new GameLogger();
		}
		return INSTANCE;
	}	
	
	
	private HashMap<String,BufferedWriter> loggerMap = new HashMap<String,BufferedWriter>();
	
	public GameLogger() {
		
	}

	public void setFiles(ArrayList<UserSetting> userList, String logFileName) {
		loggerMap.clear();
		Iterator<UserSetting> it = userList.iterator();
		while(it.hasNext()) {
			UserSetting user = it.next();
			String logFolder = "./"+user.getUserFolder();
			String userName = user.getName();
			System.out.println("Log: "+ logFolder+logFileName);
			if (!(logFileName.equals("") || logFolder.equals(""))) {
				BufferedWriter logger = null;
				File folder;
				if(logFolder.endsWith("/"))
				{
					logFolder = logFolder+"logs/";
				}
				else
				{
					logFolder = logFolder+"/logs/";
				}
				folder = new File(logFolder);
				if(!folder.exists()) {
					try {
						folder.mkdirs();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				try {
				    // Add to the desired logger
				    logger = new BufferedWriter(new FileWriter(logFolder+logFileName, true));
				   
				} catch (IOException e) {
					logger = null;
					e.printStackTrace();
				}		
				if(logger!=null) {
					loggerMap.put(userName, logger);
				}
			}
		}
	}
	
	
	public void log(String name, String data) {
		BufferedWriter logger = loggerMap.get(name);
		if (logger != null) {
			try {
				Calendar cal = Calendar.getInstance();
				//logger.write(cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND)+ ":" + cal.get(Calendar.MILLISECOND));
				logger.write(String.format("%1$te.%1$tm.%1$tY ", cal));
				logger.write(String.format("%1$tH:%1$tM:%1$tS:%1$tL", cal));
				logger.write(",");
				logger.write(data + "\n");
				logger.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			System.err.println("ei loggeria, data: " + data);
		}
	}
	
}
